<?php
namespace Core;

use Exception;
use App\Exceptions\NotFoundException;
use App\Exceptions\ValidationException;
use App\Exceptions\AuthenticationException;
use App\Exceptions\AuthorizationException;

class ExceptionHandler {
    protected $container;
    
    public function __construct(Container $container) {
        $this->container = $container;
    }
    
    public function handle(Exception $e) {
        // 处理自定义异常
        if ($e instanceof NotFoundException) {
            return $this->handleNotFoundException($e);
        }
        
        if ($e instanceof ValidationException) {
            return $this->handleValidationException($e);
        }
        
        if ($e instanceof AuthenticationException) {
            return $this->handleAuthenticationException($e);
        }
        
        if ($e instanceof AuthorizationException) {
            return $this->handleAuthorizationException($e);
        }
        
        // 处理其他异常
        return $this->handleGenericException($e);
    }
    
    protected function handleNotFoundException(NotFoundException $e) {
        $statusCode = $e->getCode();
        
        $response = [
            'error' => [
                'message' => $e->getMessage(),
                'code' => $statusCode,
                'type' => 'not_found'
            ]
        ];
        
        return Response::make()
            ->status($statusCode)
            ->json($response);
    }
    
    protected function handleValidationException(ValidationException $e) {
        $statusCode = $e->getCode();
        
        $response = [
            'error' => [
                'message' => $e->getMessage(),
                'code' => $statusCode,
                'type' => 'validation',
                'errors' => $e->getErrors()
            ]
        ];
        
        return Response::make()
            ->status($statusCode)
            ->json($response);
    }
    
    protected function handleAuthenticationException(AuthenticationException $e) {
        $statusCode = $e->getCode();
        
        $response = [
            'error' => [
                'message' => $e->getMessage(),
                'code' => $statusCode,
                'type' => 'authentication'
            ]
        ];
        
        return Response::make()
            ->status($statusCode)
            ->json($response);
    }
    
    protected function handleAuthorizationException(AuthorizationException $e) {
        $statusCode = $e->getCode();
        
        $response = [
            'error' => [
                'message' => $e->getMessage(),
                'code' => $statusCode,
                'type' => 'authorization'
            ]
        ];
        
        return Response::make()
            ->status($statusCode)
            ->json($response);
    }
    
    protected function handleGenericException(Exception $e) {
        $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500;
        
        $response = [
            'error' => [
                'message' => $e->getMessage(),
                'code' => $e->getCode(),
                'status' => $statusCode,
                'type' => 'server_error'
            ]
        ];
        
        // 在开发环境中显示详细错误信息
        if (getenv('APP_ENV') === 'development' || getenv('APP_DEBUG') === 'true') {
            $response['error']['trace'] = $e->getTrace();
            $response['error']['file'] = $e->getFile();
            $response['error']['line'] = $e->getLine();
        }
        
        return Response::make()
            ->status($statusCode)
            ->json($response);
    }
}